define(['jquery', 'bootstrap', 'frontend', 'form', 'template', 'fullcalendar'], function ($, undefined, Frontend, Form, Template, Rili) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };

    // curriculum  functions start






    // curriculum functions end




    var Controller = {
        login: function () {
            //本地验证未通过时提示
            $("#login-form").data("validator-options", validatoroptions);

            $(document).on("change", "input[name=type]", function () {
                var type = $(this).val();
                $("div.form-group[data-type]").addClass("hide");
                $("div.form-group[data-type='" + type + "']").removeClass("hide");
                $('#resetpwd-form').validator("setField", {
                    captcha: "required;length(4);integer[+];remote(" + $(this).data("check-url") + ", event=resetpwd, " + type + ":#" + type + ")",
                });
                $(".btn-captcha").data("url", $(this).data("send-url")).data("type", type);
            });

            //为表单绑定事件
            Form.api.bindevent($("#login-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });

            Form.api.bindevent($("#resetpwd-form"), function (data) {
                Layer.closeAll();
            });

            $(document).on("click", ".btn-forgot", function () {
                var id = "resetpwdtpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: __('Reset password'),
                    area: ["450px", "355px"],
                    content: content,
                    success: function (layero) {
                        Form.api.bindevent($("#resetpwd-form", layero), function (data) {
                            Layer.closeAll();
                        });
                    }
                });
            });
        },
        register: function () {
            //本地验证未通过时提示
            $("#register-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#register-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            }, function (data) {
                $("input[name=captcha]").next(".input-group-addon").find("img").trigger("click");
            });
        },
        changepwd: function () {
            //本地验证未通过时提示
            $("#changepwd-form").data("validator-options", validatoroptions);

            //为表单绑定事件
            Form.api.bindevent($("#changepwd-form"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
            });
        },
        curriculum: function () {

            /**
             * @description 移除当前显示的课程安排
             */
            function removeEvents() {
                var events = calendar.getEvents();

                for (var i = 0; i < events.length; ++i) {
                    events[i].remove();
                }
            }


            function updateTitle(month) {

                // <div class="fc-toolbar-chunk"><h2 class="fc-toolbar-title">2021年05月</h2></div>

                const toolbar = document.getElementsByClassName('fc-header-toolbar')[0];
                const chunk = toolbar.getElementsByClassName('fc-toolbar-chunk')[1];

                if (month) {
                    chunk.innerHTML = '<h2 class="fc-toolbar-title">' + month + '</h2>';
                }
            }


            /**
             * @description 重新渲染课程表
             */
            function refreshSchedule() {
                // schedule
                Fast.api.ajax({
                    url: '/api/user/time',
                    data: {
                        timestamp: calendar.getDate().getTime() / 1000,
                    }
                }, function (data) {
                    removeEvents();

                    updateTitle(data.month);

                    calendar.addEventSource(data.list);

                    calendar.refetchEvents();

                    document.getElementById('username').innerHTML = data.username;

                    if (data.class) {
                        document.getElementById('classname').innerHTML = '(' + data.class + ')';
                    }
                });
            }
            /**
             *
             * @description 重新编写日历模式，采用fullcalendar
             * @author Lvzh
             */
            var calendarEl = document.getElementById('fullcalendar');

            var calendar = new FullCalendar.Calendar(calendarEl, {
                locale: 'zh-cn',
                customButtons: {
                    gotoPrev: {
                        text: '<',
                        click: function() {
                            calendar.prev();

                            refreshSchedule();
                        }
                    },
                    gotoNext: {
                        text: '>',
                        click: function() {
                            calendar.next();

                            refreshSchedule();
                        }
                    },
                    gotoToday: {
                        text: '今天',
                        click: function() {
                            calendar.today();

                            refreshSchedule();
                        }
                    },
                },
                headerToolbar: {
                    left: 'gotoPrev gotoToday',
                    center: '',
                    right: 'gotoNext'
                },
                eventClick: function(info) {
                    document.getElementById("memo").innerHTML = info.event.extendedProps.description;
                },
                events: [],
                height: 450,
            });

            calendar.render();

            // 初始化课程表
            refreshSchedule();
        },
        profile: function () {
            // 给上传按钮添加上传成功事件
            $("#plupload-avatar").data("upload-success", function (data) {
                var url = Fast.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success(__('Upload successful'));
            });
            Form.api.bindevent($("#profile-form"));
            $(document).on("click", ".btn-change", function () {
                var that = this;
                var id = $(this).data("type") + "tpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "修改",
                    area: ["400px", "250px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            Layer.closeAll();
                        });
                    }
                });
            });
        }
    };
    return Controller;
});